#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("数据保存软件V1.0.2");

    m_thread = new DataThread;
    connect(m_thread,&DataThread::sendsize,this,&MainWindow::Slot_rev);
    connect(m_thread,&DataThread::sendprogress,this,&MainWindow::Slot_progress);
}

MainWindow::~MainWindow()
{
    delete ui;
    m_sLock.lock();
    m_end[0] = true;
    m_end[1] = true;
    m_end[2] = true;
    m_sLock.unlock();
    m_thread->quit();
    m_thread->wait();
    delete m_thread;
    m_thread = nullptr;
}


void MainWindow::on_pushButton_clicked()
{
    ui->progressBar->setValue(0);
    QString lastpath = QDir::currentPath();
    lastpath = QFileDialog::getOpenFileName(this, "open file", lastpath, "text file(*.txt);;All file(*.*)");

    QFile *localFile = new QFile(lastpath);

    if (!localFile->open((QFile::ReadOnly)))
    {
        ui->textBrowser->setTextColor(Qt::red);
        ui->textBrowser->append("[上位机]打开txt文件失败");
        return;
    }
    else
    {
        localFile->seek(0);
        //解析文件
        fileary[0].clear();
        fileary[0].append(localFile->readAll());
        localFile->close();
        m_thread->start();
    }
}

void MainWindow::Slot_rev(unsigned long long){
    ui->textBrowser->setTextColor(Qt::blue);
    ui->progressBar->setValue(100);
    ui->textBrowser->append("[上位机]保存完成");
}

void MainWindow::Slot_progress(float res){
    ui->progressBar->setValue(res * 100);
}
